<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 防抖
    // 应用场景：输入框实时搜索、窗口resize事件、按钮点击事件等
    function debounce(fn, delay) {
      let timer = null
      const args = arguments
      return function () {
        if (timer) clearTimeout(timer)
        timer = setTimeout(() => {
          fn.apply(this, args)
        }, delay)
      }
    }

    // 节流
    // 应用场景：滚动事件、鼠标移动事件等
    function throttle(fn, delay) {
      let lastTime = 0;
      if (Date.now() - lastTime > delay) {
        lastTime = Date.now()
        fn.apply(this, args)
      }
    }
  </script>
</body>

</html>